using System.IO;
using UnityEngine;

public class CreateScript : UnityEditor.AssetModificationProcessor
{
    static string modelTempletePath = Application.dataPath + "/Scripts/Editor/FastScript/ModelTemplete.txt";
    static string modulelTempletePath = Application.dataPath + "/Scripts/Editor/FastScript/ModuleTemplete.txt";
    static string uiPanelTempletePath = Application.dataPath + "/Scripts/Editor/FastScript/UIPanelTemplete.txt";
    static string uiWindowTempletePath = Application.dataPath + "/Scripts//Editor/FastScript/UIWindowTemplete.txt";
    static string nDataTempletePath = Application.dataPath + "/Scripts//Editor/FastScript/NDataTemplete.txt";
    static string mgrTempletePath = Application.dataPath + "/Scripts//Editor/FastScript/MgrTemplete.txt";
    static string subViewTempletePath = Application.dataPath + "/Scripts//Editor/FastScript/UISubViewTemplete.txt";

    private static string CreateModelScript(string scriptName)
    {
        string str = File.ReadAllText(modelTempletePath);
        str = str.Replace("#MODELNAME#", scriptName);
        return str;
    }

    private static string CreateModuleScript(string scriptName)
    {
        string str = File.ReadAllText(modulelTempletePath);
        str = str.Replace("#MODULENAME#", scriptName);
        return str;
    }

    private static string CreateUIPanelScript(string scriptName)
    {
        string str = File.ReadAllText(uiPanelTempletePath);
        str = str.Replace("#SCRIPTNAME#", scriptName);
        str = str.Replace("#PREFABPATH#", "prefabName");
        return str;
    }

    private static string CreateUIWindowScript(string scriptName)
    {
        string str = File.ReadAllText(uiWindowTempletePath);
        str = str.Replace("#WINDOWNAME#", scriptName);
        return str;
    }

    private static string CreateNDataScript(string scriptName)
    {
        string str = File.ReadAllText(nDataTempletePath);
        str = str.Replace("#NDATANAME#", scriptName);
        return str;
    }

    private static string CreateMgrScript(string scriptName)
    {
        string str = File.ReadAllText(mgrTempletePath);
        str = str.Replace("#MGR_NAME#", scriptName);
        return str;
    }

    private static string CreateSubViewScript(string scriptName)
    {
        string str = File.ReadAllText(subViewTempletePath);
        str = str.Replace("#SCRIPTNAME#", scriptName);
        return str;
    }

    private static string GetScriptName(string path)
    {
        string[] pathNames = path.Split('/');
        string scriptAllPath = pathNames[pathNames.Length - 1];
        string[] names = scriptAllPath.Split('.');
        return names[0];
    }

    public static void OnWillCreateAsset(string path)
    {
        path = path.Replace(".meta", "");
        if (path.EndsWith(".cs") && path.Contains("Assets/Scripts"))
        {
            string allText = string.Empty;
            string scriptName = GetScriptName(path);
            if (path.IndexOf("UI") != -1 && scriptName.IndexOf("Panel") != -1)
            {
                allText = CreateUIPanelScript(scriptName);
                File.WriteAllText(path, allText);
            }
            if (path.IndexOf("Model") != -1 && scriptName.IndexOf("Model") != -1)
            {
                allText = CreateModelScript(scriptName);
                File.WriteAllText(path, allText);
            }
            if (path.IndexOf("Module") != -1 && scriptName.IndexOf("Module") != -1)
            {
                allText = CreateModuleScript(scriptName);
                File.WriteAllText(path, allText);
            }
            if (path.IndexOf("UI") != -1 && scriptName.IndexOf("Window") != -1)
            {
                allText = CreateUIWindowScript(scriptName);
                File.WriteAllText(path, allText);
            }
            if (path.IndexOf("NetData") != -1 && scriptName.IndexOf("Data") != -1)
            {
                allText = CreateNDataScript(scriptName);
                File.WriteAllText(path, allText);
            }
            if (path.IndexOf("Mgr") != -1 && scriptName.IndexOf("Mgr") != -1)
            {
                allText = CreateMgrScript(scriptName);
                File.WriteAllText(path, allText);
            }
            if (path.IndexOf("UI") != -1 && scriptName.IndexOf("SubView") != -1)
            {
                allText = CreateSubViewScript(scriptName);
                File.WriteAllText(path, allText);
            }
        }
    }
}